import 'package:flutter_qjs/flutter_qjs.dart';

class JsPolyfills {
  late JavascriptRuntime runtime;
  JsPolyfills(this.runtime);

  void init() {
    runtime.evaluate('''
class FormData {
  constructor() {
    this.params = {};
  }

  append(key, value) {
    this.params[key] = value;
  }

  toJSON() {
    return this.params;
  }
}
''');
    runtime.evaluate('''
/**!
 * url-search-params-polyfill
 *
 * @author Jerry Bendy (https://github.com/jerrybendy)
 * @licence MIT
 */
!function(t){"use strict";var n,r=function(){try{if(t.URLSearchParams&&"bar"===new t.URLSearchParams("foo=bar").get("foo"))return t.URLSearchParams}catch(t){}return null}(),e=r&&"a=1"===new r({a:1}).toString(),o=r&&"+"===new r("s=%2B").get("s"),i=r&&"size"in r.prototype,a="__URLSearchParams__",c=!r||((n=new r).append("s"," &"),"s=+%26"===n.toString()),s=p.prototype,u=!(!t.Symbol||!t.Symbol.iterator);if(!(r&&e&&o&&c&&i)){s.append=function(t,n){d(this[a],t,n)},s.delete=function(t){delete this[a][t]},s.get=function(t){var n=this[a];return this.has(t)?n[t][0]:null},s.getAll=function(t){var n=this[a];return this.has(t)?n[t].slice(0):[]},s.has=function(t){return b(this[a],t)},s.set=function(t,n){this[a][t]=[""+n]},s.toString=function(){var t,n,r,e,o=this[a],i=[];for(n in o)for(r=g(n),t=0,e=o[n];t<e.length;t++)i.push(r+"="+g(e[t]));return i.join("&")};var f,h=t.Proxy&&r&&(!o||!c||!e||!i);h?(f=new Proxy(r,{construct:function(t,n){return new t(new p(n[0]).toString())}})).toString=Function.prototype.toString.bind(p):f=p,Object.defineProperty(t,"URLSearchParams",{value:f});var l=t.URLSearchParams.prototype;l.polyfill=!0,!h&&t.Symbol&&(l[t.Symbol.toStringTag]="URLSearchParams"),"forEach"in l||(l.forEach=function(t,n){var r=v(this.toString());Object.getOwnPropertyNames(r).forEach((function(e){r[e].forEach((function(r){t.call(n,r,e,this)}),this)}),this)}),"sort"in l||(l.sort=function(){var t,n,r,e=v(this.toString()),o=[];for(t in e)o.push(t);for(o.sort(),n=0;n<o.length;n++)this.delete(o[n]);for(n=0;n<o.length;n++){var i=o[n],a=e[i];for(r=0;r<a.length;r++)this.append(i,a[r])}}),"keys"in l||(l.keys=function(){var t=[];return this.forEach((function(n,r){t.push(r)})),S(t)}),"values"in l||(l.values=function(){var t=[];return this.forEach((function(n){t.push(n)})),S(t)}),"entries"in l||(l.entries=function(){var t=[];return this.forEach((function(n,r){t.push([r,n])})),S(t)}),u&&(l[t.Symbol.iterator]=l[t.Symbol.iterator]||l.entries),"size"in l||Object.defineProperty(l,"size",{get:function(){var t=v(this.toString());if(l===this)throw new TypeError("Illegal invocation at URLSearchParams.invokeGetter");return Object.keys(t).reduce((function(n,r){return n+t[r].length}),0)}})}function p(t){((t=t||"")instanceof URLSearchParams||t instanceof p)&&(t=t.toString()),this[a]=v(t)}function g(t){var n={"!":"%21","'":"%27","(":"%28",")":"%29","~":"%7E","%20":"+","%00":"\\0"};return encodeURIComponent(t).replace(/[!'\\(\\)~]|%20|%00/g,(function(t){return n[t]}))}function y(t){return t.replace(/[ +]/g,"%20").replace(/(%[a-f0-9]{2})+/gi,(function(t){return decodeURIComponent(t)}))}function S(n){var r={next:function(){var t=n.shift();return{done:void 0===t,value:t}}};return u&&(r[t.Symbol.iterator]=function(){return r}),r}function v(t){var n={};if("object"==typeof t)if(m(t))for(var r=0;r<t.length;r++){var e=t[r];if(!m(e)||2!==e.length)throw new TypeError("Failed to construct 'URLSearchParams': Sequence initializer must only contain pair elements");d(n,e[0],e[1])}else for(var o in t)t.hasOwnProperty(o)&&d(n,o,t[o]);else{0===t.indexOf("?")&&(t=t.slice(1));for(var i=t.split("&"),a=0;a<i.length;a++){var c=i[a],s=c.indexOf("=");-1<s?d(n,y(c.slice(0,s)),y(c.slice(s+1))):c&&d(n,y(c),"")}}return n}function d(t,n,r){var e="string"==typeof r?r:null!=r&&"function"==typeof r.toString?r.toString():JSON.stringify(r);b(t,n)?t[n].push(e):t[n]=[e]}function m(t){return!!t&&"[object Array]"===Object.prototype.toString.call(t)}function b(t,n){return Object.prototype.hasOwnProperty.call(t,n)}}("undefined"!=typeof global?global:"undefined"!=typeof window?window:this);
''');
    runtime.evaluate('''
!function(){function t(){this.URL=function(t,e){var n,r,i,s,o,h,c,u;function a(t,e,n){return"@"===n?"":n}if(Object.defineProperty(this,"hash",{get:function(){return n},set:function(t){return n=t.length>0?"#"+t.match(/^#*(.*)/)[1]:"",t}}),Object.defineProperty(this,"host",{get:function(){return o.length>0?r+":"+o:r},set:function(t){var e=t.split(":");return this.hostname=e[0],this.port=e[1],t}}),Object.defineProperty(this,"hostname",{get:function(){return r},set:function(t){return r=t.length>0?encodeURIComponent(t):r,t}}),Object.defineProperty(this,"href",{get:function(){var t=h+"//";return(u.length>0||i.length>0)&&(u.length>0&&(t+=u),i.length>0&&(t+=":"+i),t+="@"),t+=r,o.length>0&&(t+=":"+o),t+=s+c+n},set:function(t){this.protocol=t;var e=(t=t.replace(/.*?:\\/*/,"")).match(/([^:]*).*@/);this.username=e?e[1]:"";var n=(t=t.replace(/([^:]*):?(.*@)/,a)).match(/.*(?=@)/);this.password=n?n[0]:"",t=t.replace(/.*@/,""),this.hostname=t.match(/[^:/?]*/);var r=t.match(/:(\\d+)/);this.port=r?r[1]:"";var i=t.match(/\\/([^?#]*)/);this.pathname=i?i[1]:"";var s=t.match(/\\?[^#]*/);this.search=s?s[0]:"";var o=t.match(/\\#.*/);this.hash=o?o[0]:""}}),Object.defineProperty(this,"origin",{get:function(){var t=h+"//"+r;return o.length>0&&(t+=":"+o),t},set:function(t){this.protocol=t,t=t.replace(/.*?:\\/*/,""),this.hostname=t.match(/[^:/?]*/);var e=t.match(/:(\\d+)/);this.port=e?e[1]:""}}),Object.defineProperty(this,"password",{get:function(){return i},set:function(t){return i=encodeURIComponent(t),t}}),Object.defineProperty(this,"pathname",{get:function(){return s},set:function(t){return s="/"+t.match(/\\/?(.*)/)[1],t}}),Object.defineProperty(this,"port",{get:function(){return o},set:function(t){return o=isNaN(t)||""===t?"":Math.min(65535,t).toString(),t}}),Object.defineProperty(this,"protocol",{get:function(){return h},set:function(t){return h=t.match(/[^/:]*/)[0]+":",t}}),Object.defineProperty(this,"search",{get:function(){return c},set:function(t){return c=t.length>0?"?"+t.match(/\\??(.*)/)[1]:"",t}}),Object.defineProperty(this,"username",{get:function(){return u},set:function(t){u=t}}),"string"==typeof t){var f=/^[a-zA-z]+:\\/\\/.*/.test(t),p=/^[a-zA-z]+:\\/\\/.*/.test(e);if(f)this.href=t;else{if(!p)throw new TypeError('URL string is not valid. If using a relative url, a second argument needs to be passed representing the base URL. Example: new URL("relative/path", "http://www.example.com");');this.href=e+t}}else n=t.hash,r=t.hostname,i=t.password?t.password:"",s=t.pathname,o=t.port,h=t.protocol,c=t.search,u=t.username?t.username:"";this.searchParams=function(t){var e=[],n=[];t.search.length>0&&t.search.slice(1).split("&").forEach((function(t){var r=t.split("=");e.push(r[0]),n.push(r[1])}));function r(){0===e.length?t.search="":t.search=e.map((function(t,e){return t+"="+n[e]})).join("&")}return{append:function(t,i){e.push(t),n.push(i),r()},delete:function(t){for(;e.indexOf(t)>-1;)n.splice(e.indexOf(t),1),e.splice(e.indexOf(t),1);r()},entries:function(){return e.map((function(t,e){return[t,n[e]]}))},get:function(t){return n[e.indexOf(t)]},getAll:function(t){return n.filter((function(n,r){return e[r]===t}))},has:function(t){return e.indexOf(t)>-1},keys:function(){return e},set:function(t,i){if(-1===e.indexOf(t))this.append(t,i);else{var s=!0,o=[];e=e.filter((function(e,r){return e!==t?(o.push(n[r]),!0):!!s&&(s=!1,o.push(i),!0)})),n=o,r()}},sort:function(){var t=this.entries().sort();e=[],n=[],t.forEach((function(t){e.push(t[0]),n.push(t[1])})),r()},toString:function(){return t.search.slice(1)},values:function(){return n}}}(this)}}"function"!=typeof URL?t():"searchParams"in new URL(window.location)||t()}();
''');
    runtime.evaluate('''
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports.dayjs=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).dayjs=e()}(this,(function(){"use strict";var t=1e3,e=6e4,n=36e5,r="millisecond",i="second",s="minute",u="hour",a="day",o="week",c="month",f="quarter",h="year",d="date",l="Invalid Date",\$=/^(\\d{4})[-/]?(\\d{1,2})?[-/]?(\\d{0,2})[Tt\\s]*(\\d{1,2})?:?(\\d{1,2})?:?(\\d{1,2})?[.:]?(\\d+)?\$/,y=/\\[([^\\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,M={name:"en",weekdays:"Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),months:"January_February_March_April_May_June_July_August_September_October_November_December".split("_"),ordinal:function(t){var e=["th","st","nd","rd"],n=t%100;return"["+t+(e[(n-20)%10]||e[n]||e[0])+"]"}},m=function(t,e,n){var r=String(t);return!r||r.length>=e?t:""+Array(e+1-r.length).join(n)+t},v={s:m,z:function(t){var e=-t.utcOffset(),n=Math.abs(e),r=Math.floor(n/60),i=n%60;return(e<=0?"+":"-")+m(r,2,"0")+":"+m(i,2,"0")},m:function t(e,n){if(e.date()<n.date())return-t(n,e);var r=12*(n.year()-e.year())+(n.month()-e.month()),i=e.clone().add(r,c),s=n-i<0,u=e.clone().add(r+(s?-1:1),c);return+(-(r+(n-i)/(s?i-u:u-i))||0)},a:function(t){return t<0?Math.ceil(t)||0:Math.floor(t)},p:function(t){return{M:c,y:h,w:o,d:a,D:d,h:u,m:s,s:i,ms:r,Q:f}[t]||String(t||"").toLowerCase().replace(/s\$/,"")},u:function(t){return void 0===t}},g="en",D={};D[g]=M;var p="\$isDayjsObject",S=function(t){return t instanceof _||!(!t||!t[p])},w=function t(e,n,r){var i;if(!e)return g;if("string"==typeof e){var s=e.toLowerCase();D[s]&&(i=s),n&&(D[s]=n,i=s);var u=e.split("-");if(!i&&u.length>1)return t(u[0])}else{var a=e.name;D[a]=e,i=a}return!r&&i&&(g=i),i||!r&&g},O=function(t,e){if(S(t))return t.clone();var n="object"==typeof e?e:{};return n.date=t,n.args=arguments,new _(n)},b=v;b.l=w,b.i=S,b.w=function(t,e){return O(t,{locale:e.\$L,utc:e.\$u,x:e.\$x,\$offset:e.\$offset})};var _=function(){function M(t){this.\$L=w(t.locale,null,!0),this.parse(t),this.\$x=this.\$x||t.x||{},this[p]=!0}var m=M.prototype;return m.parse=function(t){this.\$d=function(t){var e=t.date,n=t.utc;if(null===e)return new Date(NaN);if(b.u(e))return new Date;if(e instanceof Date)return new Date(e);if("string"==typeof e&&!/Z\$/i.test(e)){var r=e.match(\$);if(r){var i=r[2]-1||0,s=(r[7]||"0").substring(0,3);return n?new Date(Date.UTC(r[1],i,r[3]||1,r[4]||0,r[5]||0,r[6]||0,s)):new Date(r[1],i,r[3]||1,r[4]||0,r[5]||0,r[6]||0,s)}}return new Date(e)}(t),this.init()},m.init=function(){var t=this.\$d;this.\$y=t.getFullYear(),this.\$M=t.getMonth(),this.\$D=t.getDate(),this.\$W=t.getDay(),this.\$H=t.getHours(),this.\$m=t.getMinutes(),this.\$s=t.getSeconds(),this.\$ms=t.getMilliseconds()},m.\$utils=function(){return b},m.isValid=function(){return!(this.\$d.toString()===l)},m.isSame=function(t,e){var n=O(t);return this.startOf(e)<=n&&n<=this.endOf(e)},m.isAfter=function(t,e){return O(t)<this.startOf(e)},m.isBefore=function(t,e){return this.endOf(e)<O(t)},m.\$g=function(t,e,n){return b.u(t)?this[e]:this.set(n,t)},m.unix=function(){return Math.floor(this.valueOf()/1e3)},m.valueOf=function(){return this.\$d.getTime()},m.startOf=function(t,e){var n=this,r=!!b.u(e)||e,f=b.p(t),l=function(t,e){var i=b.w(n.\$u?Date.UTC(n.\$y,e,t):new Date(n.\$y,e,t),n);return r?i:i.endOf(a)},\$=function(t,e){return b.w(n.toDate()[t].apply(n.toDate("s"),(r?[0,0,0,0]:[23,59,59,999]).slice(e)),n)},y=this.\$W,M=this.\$M,m=this.\$D,v="set"+(this.\$u?"UTC":"");switch(f){case h:return r?l(1,0):l(31,11);case c:return r?l(1,M):l(0,M+1);case o:var g=this.\$locale().weekStart||0,D=(y<g?y+7:y)-g;return l(r?m-D:m+(6-D),M);case a:case d:return \$(v+"Hours",0);case u:return \$(v+"Minutes",1);case s:return \$(v+"Seconds",2);case i:return \$(v+"Milliseconds",3);default:return this.clone()}},m.endOf=function(t){return this.startOf(t,!1)},m.\$set=function(t,e){var n,o=b.p(t),f="set"+(this.\$u?"UTC":""),l=(n={},n[a]=f+"Date",n[d]=f+"Date",n[c]=f+"Month",n[h]=f+"FullYear",n[u]=f+"Hours",n[s]=f+"Minutes",n[i]=f+"Seconds",n[r]=f+"Milliseconds",n)[o],\$=o===a?this.\$D+(e-this.\$W):e;if(o===c||o===h){var y=this.clone().set(d,1);y.\$d[l](\$),y.init(),this.\$d=y.set(d,Math.min(this.\$D,y.daysInMonth())).\$d}else l&&this.\$d[l](\$);return this.init(),this},m.set=function(t,e){return this.clone().\$set(t,e)},m.get=function(t){return this[b.p(t)]()},m.add=function(r,f){var d,l=this;r=Number(r);var \$=b.p(f),y=function(t){var e=O(l);return b.w(e.date(e.date()+Math.round(t*r)),l)};if(\$===c)return this.set(c,this.\$M+r);if(\$===h)return this.set(h,this.\$y+r);if(\$===a)return y(1);if(\$===o)return y(7);var M=(d={},d[s]=e,d[u]=n,d[i]=t,d)[\$]||1,m=this.\$d.getTime()+r*M;return b.w(m,this)},m.subtract=function(t,e){return this.add(-1*t,e)},m.format=function(t){var e=this,n=this.\$locale();if(!this.isValid())return n.invalidDate||l;var r=t||"YYYY-MM-DDTHH:mm:ssZ",i=b.z(this),s=this.\$H,u=this.\$m,a=this.\$M,o=n.weekdays,c=n.months,f=n.meridiem,h=function(t,n,i,s){return t&&(t[n]||t(e,r))||i[n].slice(0,s)},d=function(t){return b.s(s%12||12,t,"0")},\$=f||function(t,e,n){var r=t<12?"AM":"PM";return n?r.toLowerCase():r};return r.replace(y,(function(t,r){return r||function(t){switch(t){case"YY":return String(e.\$y).slice(-2);case"YYYY":return b.s(e.\$y,4,"0");case"M":return a+1;case"MM":return b.s(a+1,2,"0");case"MMM":return h(n.monthsShort,a,c,3);case"MMMM":return h(c,a);case"D":return e.\$D;case"DD":return b.s(e.\$D,2,"0");case"d":return String(e.\$W);case"dd":return h(n.weekdaysMin,e.\$W,o,2);case"ddd":return h(n.weekdaysShort,e.\$W,o,3);case"dddd":return o[e.\$W];case"H":return String(s);case"HH":return b.s(s,2,"0");case"h":return d(1);case"hh":return d(2);case"a":return \$(s,u,!0);case"A":return \$(s,u,!1);case"m":return String(u);case"mm":return b.s(u,2,"0");case"s":return String(e.\$s);case"ss":return b.s(e.\$s,2,"0");case"SSS":return b.s(e.\$ms,3,"0");case"Z":return i}return null}(t)||i.replace(":","")}))},m.utcOffset=function(){return 15*-Math.round(this.\$d.getTimezoneOffset()/15)},m.diff=function(r,d,l){var \$,y=this,M=b.p(d),m=O(r),v=(m.utcOffset()-this.utcOffset())*e,g=this-m,D=function(){return b.m(y,m)};switch(M){case h:\$=D()/12;break;case c:\$=D();break;case f:\$=D()/3;break;case o:\$=(g-v)/6048e5;break;case a:\$=(g-v)/864e5;break;case u:\$=g/n;break;case s:\$=g/e;break;case i:\$=g/t;break;default:\$=g}return l?\$:b.a(\$)},m.daysInMonth=function(){return this.endOf(c).\$D},m.\$locale=function(){return D[this.\$L]},m.locale=function(t,e){if(!t)return this.\$L;var n=this.clone(),r=w(t,e,!0);return r&&(n.\$L=r),n},m.clone=function(){return b.w(this.\$d,this)},m.toDate=function(){return new Date(this.valueOf())},m.toJSON=function(){return this.isValid()?this.toISOString():null},m.toISOString=function(){return this.\$d.toISOString()},m.toString=function(){return this.\$d.toUTCString()},M}(),k=_.prototype;return O.prototype=k,[["\$ms",r],["\$s",i],["\$m",s],["\$H",u],["\$W",a],["\$M",c],["\$y",h],["\$D",d]].forEach((function(t){k[t[1]]=function(e){return this.\$g(e,t[0],t[1])}})),O.extend=function(t,e){return t.\$i||(t(e,_,O),t.\$i=!0),O},O.locale=w,O.isDayjs=S,O.unix=function(t){return O(1e3*t)},O.en=D[g],O.Ls=D,O.p={},O}));
''');
  }
}
